<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src/ux/Ux.Sign.js - Zero UI Doc</title>
    <meta name="description" content="Zero UI前端文档工具">
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="stylesheet" href="../assets/css/custom.css">
    <link rel="stylesheet" href="../assets/vendor/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="../assets/vendor/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../assets/css/theme.css">
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,600,700" rel="stylesheet" type="text/css">
    <link rel="shortcut icon" type="image/png" href="../assets/img/favicon.png">
    
</head>
<body>
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <div class="navbar-header">
          <a href="../" class="navbar-brand">
            <img src="../assets/img/mdeditor-logo.png" alt="">
            <span>API Docs</span>
          </a>
        </div>

        <div class="collapse navbar-collapse" id="nav">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="https://github.com/silentbalanceyh/vertx-ui.git" class="fa fa-github github"></a></li>
          </ul>
        </div>
      </div>
    </nav>
    <div id="main-wrapper" class="row">
        <div id="content-wrapper">
            <ol class="panel-group" id="sidebar" role="tablist" aria-multiselectable="true">
                <li class="panel panel-default">
                    <div class="panel-heading" role="tab" id="sidebar-search-heading">
                        <h4 class="panel-title">
                      <a role="button" data-toggle="collapse" href="#sidebar-search" aria-expanded="true" aria-controls="collapseOne">
                        Search
                      </a>
                    </h4>
                    </div>
                    <div id="sidebar-search" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="sidebar-search-heading">
                        <div class="panel-body">
                            <div id="api-tabview-filter">
                                <input type="search" id="api-filter" placeholder="Search...">
                            </div>
                        </div>
                    </div>
                </li>
                    <li class="panel panel-default">
                        <div class="panel-heading" role="tab" id="sidebar-version-heading">
                            <h4 class="panel-title">
                                <a role="button" href="https://github.com/silentbalanceyh/vertx-ui.git/commits/0" target="_blank">
                                  Tag: 0.1.0
                                </a>
                            </h4>
                        </div>
                    </li>
                <li class="panel panel-default">
                    <div class="panel-heading" role="tab" id="sidebar-modules-heading">
                        <h4 class="panel-title">
                    <a role="button" data-toggle="collapse" href="#sidebar-modules" aria-expanded="true" aria-controls="collapseOne">
                      Modules
                    </a>
                  </h4>
                    </div>
                    <div id="sidebar-modules" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="sidebar-modules-heading">
                        <div class="panel-body">
                                <ol>
                                        <li>
                                            <a href="../modules/Ux.html">Ux</a>
                                        </li>
                                </ol>
                        </div>
                    </div>
                </li>
            
                <li class="panel panel-default">
                    <div class="panel-heading" role="tab" id="sidebar-classes-heading">
                        <h4 class="panel-title">
                      <a role="button" data-toggle="collapse" href="#sidebar-classes" aria-expanded="true" aria-controls="collapseOne">
                        Classes
                      </a>
                    </h4>
                    </div>
                    <div id="sidebar-classes" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="sidebar-classes-heading">
                        <div class="panel-body">
                            <ol>
                                    <li>
                                        <a href="../classes/Action.html">Action</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Ajax.html">Ajax</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Attribute.html">Attribute</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Column.html">Column</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Debug.html">Debug</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Depend.html">Depend</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Dialog.html">Dialog</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Encrypt.html">Encrypt</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Env.html">Env</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Expr.html">Expr</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Field.html">Field</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Format.html">Format</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Global.html">Global</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Hoc.html">Hoc</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Html.html">Html</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Icon.html">Icon</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Jsx.html">Jsx</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Log.html">Log</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Normalize.html">Normalize</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Op.html">Op</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Option.html">Option</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Param.html">Param</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Prop.html">Prop</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Random.html">Random</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Redux.html">Redux</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Sorter.html">Sorter</a>
                                    </li>
                                    <li>
                                        <a href="../classes/State.html">State</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Store.html">Store</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Type.html">Type</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Validator.html">Validator</a>
                                    </li>
                                    <li>
                                        <a href="../classes/Value.html">Value</a>
                                    </li>
                            </ol>
                        </div>
                    </div>
                </li>
            </ol>
            <div class="content-container">
                <div class="apidocs">
                    <div id="docs-main">
                        <div class="content">
<div class="page-header">
    <h1><i class="fa fa-file-code-o" aria-hidden="true"></i>  File</h1>
</div>

<div class="file">
    <pre class="prettyprint linenums">
import Global from &#x27;./Ux.Global&#x27;;
import Expr from &#x27;./Ux.Expr&#x27;;
import Encrypt from &#x27;./Ux.Encrypt&#x27;;
import Log from &#x27;./Ux.Log&#x27;;
import Dg from &#x27;./Ux.Debug&#x27;;

const SCHEMA = {
    OAuth: () =&gt; {
        const user = Global.isLogged();
        if (user) {
            const value = Encrypt.encryptBase64(&#x60;${user.uniqueId}:${user.token}&#x60;);
            return &#x60;Bearer ${value}&#x60;
        }
    },
    Basic: () =&gt; {
        const user = Global.isLogged();
        if (user) {
            return &quot;Basic &quot; + user.token
        }
    }
};

const _parameters = (params = {}) =&gt; {
    let param = &quot;&quot;;
    const keys = Object.keys(params).sort();
    if (0 &lt; keys.length) {
        keys.forEach(key =&gt; {
            if (&quot;pager&quot; === key) {
                // 1.Pager参数专用签名
                let pager = params[key];
                if (&quot;string&quot; === typeof params[key]) {
                    pager = JSON.parse(params[key]);
                }
                if (pager) {
                    let sign = &#x60;:index${pager.index}size${pager.size}&#x60;;
                    param += key + sign + &quot;:&quot;;
                } else {
                    param += key + &quot;:&quot;;
                }
            } else {
                // 这两个参数不参加签名
                if (&quot;criterias&quot; !== key &amp;&amp; &quot;sig&quot; !== key) {
                    if (params[key]) {
                        if (&quot;object&quot; === typeof params[key]) {
                            param += key + JSON.stringify(params[key]) + &quot;:&quot;;
                        } else {
                            param += key + params[key] + &quot;:&quot;;
                        }
                    } else {
                        // 特殊Boolean值的签名
                        if (false === params[key]) {
                            param += key + &quot;false:&quot;;
                        } else if (undefined !== params[key]) {
                            param += key + params[key] + &quot;:&quot;;
                        }
                    }
                }
            }
        });
    }
    return param;
};
const _secret = () =&gt; {
    const user = Global.isLogged();
    let secret = &quot;&quot;;
    if (user &amp;&amp; &quot;object&quot; === typeof user) {
        // 登录后的secret为用户ID
        secret += user.uniqueId;
    } else {
        // 登录前的secret则是时间戳
        secret += Expr.formatNow(&quot;YYYYMMDDHH&quot;);
    }
    return secret;
};
/**
 * 读取Token信息
 * @method token
 * @return {*}
 */
const token = () =&gt; {
    const app = Global.isInit();
    Dg.ensureApp(app);
    const auth = app.auth;
    const fnExecute = SCHEMA[auth];
    return fnExecute();
};
/**
 * 数字签名函数
 * @method signature
 * @param {String} uri Ajax访问专用Uri
 * @param method Http方法
 * @param params Http参数
 */
const signature = (uri, method = &quot;GET&quot;, params = {}) =&gt; {
    // 构造签名的method和参数
    let seed = method.toUpperCase();
    seed += &quot;:&quot;;
    seed += _parameters(params);
    seed += uri;
    seed += &quot;$&quot;;
    // 构造secret
    const secret = _secret();
    // Seed中是否追加用户登录ID
    const user = Global.isLogged();
    if (user &amp;&amp; &quot;object&quot; === typeof user) {
        seed += user.key;
    }
    // 签名
    const sig = Encrypt.encryptHmac512(seed, secret);
    Log.sign(uri, method, params, {sig, secret, seed});
    params[&#x27;sig&#x27;] = sig;
};
export default {
    // 签名专用函数
    signature,
    // 读取当前用户登录过后的token，如果未登录则会返回undefined
    token
}

    </pre>
</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../assets/vendor/prettify/prettify-min.js"></script>
    <script>prettyPrint();</script>
    <script src="../assets/vendor/jquery/jquery.min.js"></script>
    <script src="../assets/vendor/jquery-ui/jquery-ui.min.js"></script>
    <script src="../assets/vendor/bootstrap/js/bootstrap.js"></script>
    <script src="../assets/vendor/github-slugger/slugger.js"></script>
    <script src="../assets/js/yuidoc-bootstrap.js"></script>
</body>
</html>
